﻿using DAL;
using FubiManger.Controls;
using Model;
using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FubiManger.Controls;

namespace FubiManger.UI
{
    partial class OrderfoodForm : Smobiler.Core.Controls.MobileForm
    {
        public OrderfoodForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        /// <summary>
        /// 商家信息
        /// </summary>
        ms_merchant merchant = new ms_merchant();
        /// <summary>
        /// 商家ID
        /// </summary>
        public string merchantid { get; set; }
        /// <summary>
        /// 商品集合
        /// </summary>
        List<ms_goods> gdlist = new List<ms_goods>();
        /// <summary>
        /// 购物车
        /// </summary>
        List<Gdpro> carlist = new List<Gdpro>();
        /// <summary>
        /// 购物车商品数量
        /// </summary>
        private int buycount = 0;
        private double paymoney = 0;
        /// <summary>
        /// 用餐人数
        /// </summary>
        private int person = 0;
        private ms_user user = new ms_user();
        /// <summary>
        /// 桌号名称
        /// </summary>
        public string diskname { get; set; }
        /// <summary>
        /// 用户ID
        /// </summary>
        public string adminid { get; set; }
        /// <summary>
        /// 默认到店
        /// </summary>
        private string u_sage = "1";
        private void panclose_Press(object sender, EventArgs e)
        {
            this.Close();
        }

        private void OrderfoodForm_Load(object sender, EventArgs e)
        {
            //Choosecontrol con = new Choosecontrol();
            //this.Form.ShowDialog(con, (obj, args) =>
            //{
            //    if (!string.IsNullOrEmpty(con.u_sage))
            //        u_sage = con.u_sage;
            merchant = merchantDal.Querybyid(merchantid);
            List<ms_classify> clist = classifyDal.Querybymerid(merchantid);//商家ID
            for (int i = 0; i < clist.Count; i++)
            {
                businessmenucontrol con1 = new businessmenucontrol();
                con1.businname = clist[i].title;
                con1.imgnmae = "";
                con1.clsid = clist[i].id;
                con1.stype = "2";
                con1.Controls.Find("btnvalue").BackColor = System.Drawing.Color.Transparent;
                Button btn = (Button)con1.Controls.Find("btnvalue");
                btn.ForeColor = System.Drawing.Color.DimGray;
                panmenu.Controls.Add(con1);
            }
            //});

        }

        /// <summary>
        /// 移除背景样式
        /// </summary>
        /// <param name="bt"></param>
        private void RemoveImgStyle(Image img)
        {

            img.ResourceID = "";
        }

        /// <summary>
        /// 移除全部按钮的样式
        /// </summary>
        public void RemoveAllImgStyle()
        {
            foreach (var con in panmenu.Controls)
            {
                Image img = (Image)((MobileUserControl)con).Controls.Find("imgmenu", false);
                Button btn = (Button)((MobileUserControl)con).Controls.Find("btnvalue", false);
                btn.ForeColor = System.Drawing.Color.DimGray;
                RemoveImgStyle((Image)img);

            }
        }
        /// <summary>
        /// 修改按钮样式
        /// </summary>
        /// <param name="button"></param>
        public void ChangeImgStyle(Image img)
        {

            img.ResourceID = "busin_menu";
        }
        /// <summary>
        /// 根据商家菜单改变内容
        /// </summary>
        /// <param name="name"></param>
        public void Show_name(string name, string clsid)
        {
            if (panpro.Controls.Count > 0)
                panpro.Controls.Clear();
            gdlist = goodsDal.Querybyclassid(clsid, merchantid, u_sage);//通过分类ID查线上商品列表
            panpro.Height = gdlist.Count * 82;
            for (int i = 0; i < gdlist.Count; i++)
            {
                Prodouctcontrol pcon = new Prodouctcontrol();
                pcon.cover = gdlist[i].cover;//封面
                pcon.describes = gdlist[i].describes;//描述
                pcon.gdname = gdlist[i].goods_name;//商品名称
                pcon.price = Convert.ToDouble(gdlist[i].price);//销售价
                pcon.market_price = "￥" + gdlist[i].market_price;//市场价
                pcon.gdid = gdlist[i].id;//商品ID
                pcon.stype = "2";//点餐                     
                panpro.Controls.Add(pcon);
            }

        }
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <param name="count"></param>
        /// <param name="img"></param>
        public void Addpro(string name, string cover, double money, int num)
        {
            Gdpro pro = new Gdpro();
            pro.proimg = cover;
            pro.proname = name;
            pro.price = money;
            pro.count = num;
            int index = carlist.FindIndex(x => x.proname == name);
            if (index == -1)
                carlist.Add(pro);
            else
            {
                carlist[index].count++;
            }

            if (carlist.Count > 0)
            {
                int count = 0;
                double price = 0;
                for (int i = 0; i < carlist.Count; i++)
                {
                    count += carlist[i].count;
                    price += (carlist[i].price * carlist[i].count);
                }
                buycount = count;
                lblcount.Text = buycount.ToString();
                paymoney = price;//支付金额
                                 // lblpaymoney.Text = "￥" + price;//价格
            }
            else
            {
                buycount = 0;
                paymoney = 0;//支付金额
                lblcount.Text = buycount.ToString();
                // lblpaymoney.Text = "￥0.00";//价格
            }
        }
        /// <summary>
        /// 改变购物车数据
        /// </summary>
        /// <param name="index"></param>
        /// <param name="pro"></param>
        public void Changebuycar(int index, Gdpro pro)
        {
            carlist[index] = pro;
            if (carlist.Count > 0)
            {
                int count = 0;
                double price = 0;
                for (int i = 0; i < carlist.Count; i++)
                {
                    count += carlist[i].count;
                    price += (carlist[i].price * carlist[i].count);
                }
                buycount = count;
                lblcount.Text = buycount.ToString();
                //lblpaymoney.Text = "￥" + price;//价格
                paymoney = price;//支付金额
            }
            else
            {
                buycount = 0;
                lblcount.Text = buycount.ToString();
                // lblpaymoney.Text = "￥0.00";//价格
                paymoney = 0;//支付金
            }
        }
        /// <summary>
        /// 删除购物车商品
        /// </summary>
        /// <param name="proid"></param>
        public void Delpro(int proid)
        {
            carlist.RemoveAt(proid);
            if (carlist.Count > 0)
            {
                int count = 0;
                double price = 0;
                for (int i = 0; i < carlist.Count; i++)
                {
                    count += carlist[i].count;
                    price += (carlist[i].price * carlist[i].count);
                }
                buycount = count;
                lblcount.Text = buycount.ToString();
                // lblpaymoney.Text = "￥" + price;//价格
                paymoney = price;//支付金额
            }
            else
            {
                buycount = 0;
                lblcount.Text = buycount.ToString();
                // lblpaymoney.Text = "￥0.00";//价格
                paymoney = 0;//支付金额
            }
            if (buycount > 0)
            {
                DialogOptions options = new DialogOptions(LayoutJustifyAlign.FlexEnd);
                options.Padding = Padding.Empty;
                Buycarcontrol con = new Buycarcontrol();
                con.gdpros = carlist;//商品集合
                this.Form.ShowDialog(con, options);
            }
        }
        /// <summary>
        /// 下单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnfrim_Press(object sender, EventArgs e)
        {
            if (paymoney > 0)
            {
                //if (!string.IsNullOrEmpty(diskname))
                //{
                //    double seatmoney = merchantDal.Queryseatbyid(merchantid);//查餐位费
                //    Gdpro pro = new Gdpro();
                //    pro.proimg = "";
                //    pro.proname = "餐位费";
                //    pro.price = person*seatmoney;//餐位费用
                //    pro.count = person ;
                //    carlist.Add(pro);
                //    paymoney += pro.price;//支付金额等于商品金额+餐位费
                //}
                FrimForm frm = new FrimForm();
                frm.gdlsit = carlist;//商品集合
                frm.money = paymoney;//订单金额
                frm.merchantid = merchantid;//商家ID
                frm.user = user;//用户信息
                frm.disksn = diskname;//桌号
                frm.adminid = adminid;//用户ID
                frm.u_sage = u_sage;//到店或物流
                this.Form.Show(frm, true, (obj, args) =>
                {
                    //if (frm.paystatus == true)
                    //{
                    //    lblcount.Text = "0";
                    //    lblpaymoney.Text = "￥0.00";
                    //    carlist = new List<Gdpro>();
                    //    this.Close();
                    //}
                });
            }
        }
        /// <summary>
        /// 购物车
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pancar_Press(object sender, EventArgs e)
        {
            if (buycount > 0)
            {
                DialogOptions options = new DialogOptions(LayoutJustifyAlign.FlexEnd);
                options.Padding = Padding.Empty;
                Buycarcontrol con = new Buycarcontrol();
                con.gdpros = carlist;//商品集合
                this.Form.ShowDialog(con, options);
            }
        }

    }
}
